package com.ee.chat.common.service.impl;

import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson.JSONObject;
import com.ee.chat.common.bean.Result;
import com.ee.chat.common.dao.*;
import com.ee.chat.common.entity.*;
import com.ee.chat.common.service.IPayService;
import com.ndhutu.ee.util.DateUtil;
import com.ndhutu.ee.util.FileUtil;
import com.ndhutu.ee.util.StringUtil;
import com.ndhutu.ee.util.pay.Notify;
import com.ndhutu.ee.util.pay.ali.AliPayConfig;
import com.ndhutu.ee.util.pay.ali.AliPayUtil;
import com.ndhutu.ee.util.pay.apple.ApplePayConfig;
import com.ndhutu.ee.util.pay.apple.ApplePayUtil;
import com.ndhutu.ee.util.pay.fuyilai.FYLPayConfig;
import com.ndhutu.ee.util.pay.fuyilai.FYLPayUtil;
import com.ndhutu.ee.util.pay.pay96.Pay96Config;
import com.ndhutu.ee.util.pay.pay96.Pay96Util;
import com.ndhutu.ee.util.pay.wx.WxPayConfig;
import com.ndhutu.ee.util.pay.wx.WxPayUtil;
import com.ndhutu.ee.util.pay.yfk.YFKPayConfig;
import com.ndhutu.ee.util.pay.yfk.YFKPayUtil;
import com.ndhutu.ee.util.pay.zfpai.ZFPaiPayConfig;
import com.ndhutu.ee.util.pay.zfpai.ZFPaiPayUtil;
import com.ndhutu.ee.util.pay.zunpay.ZunPayConfig;
import com.ndhutu.ee.util.pay.zunpay.ZunPayUtil;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Component
public class PayServiceImpl implements IPayService {
    @Value("${spring.profiles.active}")
    String active;
    @Value("${pay.money}")
    Integer money;
    @Value("${pay.success_to_url}")
    String successToUrl;
    @Value("${pay.false_to_url}")
    String falseToUrl;

    @Value("${pay.ali.gateway}")
    String alipayGateway;
    @Value("${pay.ali.notify_url}")
    String alipayNotifyUrl;
    @Value("${pay.ali.charset}")
    String alipayCharset;
    @Value("${pay.ali.payer_show_name}")
    String alipayPayerShowName;

    @Value("${pay.wx.notify_url}")
    String wxpayNotifyUrl;
    @Value("${pay.wx.spbill_create_ip}")
    String wxpaySpbillCreateIp;

    @Value("${pay.apple.pay_verify_url}")
    String applePayVerifyUrl;

    @Value("${pay.zfpai.pay_url}")
    String zfpaiPayUrl;
    @Value("${pay.zfpai.notify_url}")
    String zfpaiNotifyUrl;

    @Value("${pay.zunpay.notify_url}")
    String zunpayNotifyUrl;

    @Value("${pay.pay96.notify_url}")
    String pay96NotifyUrl;

    @Value("${pay.fuyilai.notify_url}")
    String fuyilaiNotifyUrl;

    @Resource
    PayChannelConfigMapper payChannelConfigMapper;
    @Resource
    SysOrderMapper sysOrderMapper;
    @Resource
    UserMapper userMapper;
    @Resource
    PayChannelMapper payChannelMapper;
    @Resource
    SysConfigMapper sysConfigMapper;
    @Resource
    UserRechargeLogMapper userRechargeLogMapper;
    @Resource
    UserSetMapper userSetMapper;

    /**
     * 获取支付渠道config
     * @param payChannelConfig
     * @return
     * @throws Exception
     */
    public Object getPayConfig(PayChannelConfig payChannelConfig) throws Exception{
        if(payChannelConfig.getType() == 0){    //原生支付
            if(payChannelConfig.getChannelId() == 1){ //支付宝APP
                AliPayConfig aliPayConfig = new AliPayConfig() {
                    @Override
                    public String getGateway() {
                        return alipayGateway;
                    }

                    @Override
                    public String getNotifyUrl() {
                        return alipayNotifyUrl;
                    }

                    @Override
                    public String getAppid() {
                        return payChannelConfig.getAppId();
                    }

                    @Override
                    public String getAppPrivateKey() {
                        return payChannelConfig.getAlipayAppPrivateKey();
                    }

                    @Override
                    public String getAlipayPublicKey() {
                        return payChannelConfig.getAlipayPublicKey();
                    }

                    @Override
                    public String getCharset() {
                        return alipayCharset;
                    }

                    @Override
                    public String getSignType() {
                        return payChannelConfig.getAlipayRsaType();
                    }

                    @Override
                    public String getAppCertPath() {
                        String url = payChannelConfig.getAlipayAppPublicKeyCrtPath();
                        if(StringUtil.isEmpty(url)){
                            return null;
                        }
                        String path = url.substring(url.indexOf("/", 8), url.lastIndexOf("/"));
                        String fileName = url.substring(url.lastIndexOf("/")+1);

                        File file = new File(path+"/"+fileName);
                        if (!file.exists()) {
                            FileUtil.syncDownload(url, path);
                        }

                        return path+"/"+fileName;
                    }

                    @Override
                    public String getAlipayCertPath() {
                        String url = payChannelConfig.getAlipayPublicKeyCrtPath();
                        if(StringUtil.isEmpty(url)){
                            return null;
                        }
                        String path = url.substring(url.indexOf("/", 8), url.lastIndexOf("/"));
                        String fileName = url.substring(url.lastIndexOf("/")+1);

                        File file = new File(path+"/"+fileName);
                        if (!file.exists()) {
                            FileUtil.syncDownload(url, path);
                        }

                        return path+"/"+fileName;
                    }

                    @Override
                    public String getAlipayRootCertPath() {
                        String url = payChannelConfig.getAlipayRootCrtPath();
                        if(StringUtil.isEmpty(url)){
                            return null;
                        }
                        String path = url.substring(url.indexOf("/", 8), url.lastIndexOf("/"));
                        String fileName = url.substring(url.lastIndexOf("/")+1);

                        File file = new File(path+"/"+fileName);
                        if (!file.exists()) {
                            FileUtil.syncDownload(url, path);
                        }

                        return path+"/"+fileName;
                    }

                    @Override
                    public String getPayerShowName() {
                        return alipayPayerShowName;
                    }
                };
                return aliPayConfig;

            }else if(payChannelConfig.getChannelId() == 2){   //微信APP
                WxPayConfig wxPayConfig = new WxPayConfig() {
                    @Override
                    public String getNotifyUrl() {
                        return wxpayNotifyUrl;
                    }

                    @Override
                    public String getSpbillCreateIp() {
                        return wxpaySpbillCreateIp;
                    }

                    @Override
                    public String getApiCrt() {
                        return payChannelConfig.getWxpayApiCrt();
                    }

                    @Override
                    public String getApiCrtKey() {
                        return payChannelConfig.getWxpayApiCrtKey();
                    }

                    @Override
                    public String getAppID() {
                        return payChannelConfig.getAppId();
                    }

                    @Override
                    public String getMchID() {
                        return payChannelConfig.getAccountNumber();
                    }

                    @Override
                    public String getKey() {
                        return payChannelConfig.getWxpayApiKey();
                    }

                    @Override
                    public InputStream getCertStream() {
                        return null;
                    }

                    /**
                     * HTTP(S) 连接超时时间，单位毫秒
                     */
                    @Override
                    public int getHttpConnectTimeoutMs() {
                        return 6*1000;
                    }

                    /**
                     * HTTP(S) 读数据超时时间，单位毫秒
                     */
                    @Override
                    public int getHttpReadTimeoutMs() {
                        return 6*1000;
                    }
                };
                return wxPayConfig;

            }else if(payChannelConfig.getChannelId() == 3){ //Apple
                ApplePayConfig applePayConfig = new ApplePayConfig() {
                    @Override
                    public String getPayVerifyUrl() {
                        return applePayVerifyUrl;
                    }
                };
                return applePayConfig;

            }
        }else if(payChannelConfig.getType() == 1){  //支付派
            ZFPaiPayConfig zfPaiPayConfig = new ZFPaiPayConfig() {
                @Override
                public String getSecret() {
                    return payChannelConfig.getAlipayAppPrivateKey();
                }

                @Override
                public String getCustomerNumber() {
                    return payChannelConfig.getAccountNumber();
                }

                @Override
                public String getPayUrl() {
                    return zfpaiPayUrl;
                }

                @Override
                public String getNotifyUrl() {
                    return zfpaiNotifyUrl;
                }

                @Override
                public String getSuccessToUrl() {
                    return successToUrl;
                }

                @Override
                public String getFalseToUrl() {
                    return falseToUrl;
                }
            };
            return zfPaiPayConfig;

        }else if(payChannelConfig.getType() == 2){  //云支付
            YFKPayConfig yfkPayConfig = new YFKPayConfig() {
                @Override
                public String getAppKey() {
                    return payChannelConfig.getAccountNumber();
                }

                @Override
                public String getAppSecret() {
                    return payChannelConfig.getAlipayAppPrivateKey();
                }
            };
            return yfkPayConfig;

        }else if(payChannelConfig.getType() == 3){  //尊付
            ZunPayConfig zunPayConfig = new ZunPayConfig() {
                @Override
                public String getMchId() {
                    return payChannelConfig.getAppId();
                }

                @Override
                public String getSecondMchId() {
                    return payChannelConfig.getAccountNumber();
                }

                @Override
                public String getPayType() {
                    return payChannelConfig.getAlipayRsaType();
                }

                @Override
                public String getNotifyUrl() {
                    return zunpayNotifyUrl;
                }

                @Override
                public String getCallbackUrl() {
                    return successToUrl;
                }

                @Override
                public String getKey() {
                    return payChannelConfig.getAlipayAppPrivateKey();
                }
            };
            return zunPayConfig;

        }else if(payChannelConfig.getType() == 4){  //96支付
            Pay96Config pay96Config = new Pay96Config() {
                @Override
                public String getMemberid() {
                    return payChannelConfig.getAccountNumber();
                }

                @Override
                public String getApiKey() {
                    return payChannelConfig.getAlipayAppPrivateKey();
                }

                @Override
                public String getNotifyUrl() {
                    return pay96NotifyUrl;
                }

                @Override
                public String getCallbackUrl() {
                    return successToUrl;
                }
            };
            return pay96Config;

        }else if(payChannelConfig.getType() == 5){  //付易来
            FYLPayConfig fylPayConfig = new FYLPayConfig() {
                @Override
                public String getAppid() {
                    return payChannelConfig.getAccountNumber();
                }

                @Override
                public String getNotifyUrl() {
                    return fuyilaiNotifyUrl;
                }

                @Override
                public String getReturnUrl() {
                    return successToUrl;
                }

                @Override
                public String getKey() {
                    return payChannelConfig.getAlipayAppPrivateKey();
                }
            };
            return fylPayConfig;
        }

        return null;
    }

    /**
     * 支付下单处理
     * @param userId    用户id
     * @param channelId 支付渠道id ，1-支付宝  1-微信
     * @param payChannelConfigId    支付渠道配置id，可为空
     * @param amount    金额，单位分
     * @param body  商品描述
     * @throws Exception
     */
    public Map<String, Object> orderHandle(Integer userId, Integer channelId, Integer payChannelConfigId, Integer amount, String orderNumber, String body) throws Exception{
        Map<String, String> order = new HashMap<>();
        String url = null;

        PayChannelConfig payChannelConfig;
        if(payChannelConfigId == null){
            payChannelConfig = payChannelConfigMapper.selectOne(channelId, null);
        }else{
            payChannelConfig = payChannelConfigMapper.selectById(payChannelConfigId);
        }

        if(payChannelConfig == null){
            throw new Exception("暂无可用支付渠道");
        }

        if("dev".equals(active)){
            amount = money;
        }

        if(payChannelConfig.getType() == 0){    //原生支付
            if(payChannelConfig.getChannelId() == 1){ //支付宝APP
                AliPayConfig aliPayConfig = (AliPayConfig) getPayConfig(payChannelConfig);

                if(payChannelConfig.getAlipayPublicKeyType() == 1){ //公钥模式
                    url = AliPayUtil.order(aliPayConfig, orderNumber, amount, body, body);

                }else if(payChannelConfig.getAlipayPublicKeyType() == 2){   //证书模式
                    url = AliPayUtil.crtOrder(aliPayConfig, orderNumber, amount, body, body);

                }
            }else if(payChannelConfig.getChannelId() == 2){   //微信APP
                WxPayConfig wxPayConfig = (WxPayConfig) getPayConfig(payChannelConfig);

                order = WxPayUtil.order(wxPayConfig, orderNumber, amount, body, body);

            }else if(payChannelConfig.getChannelId() == 3){   //apple支付
                order.put("orderNumber", orderNumber);

            }
        }else if(payChannelConfig.getType() == 1){  //支付派
            ZFPaiPayConfig zfPaiPayConfig = (ZFPaiPayConfig) getPayConfig(payChannelConfig);

            Map<String, Object> result = null;
            if(payChannelConfig.getChannelId() == 1) { //支付宝
                result = ZFPaiPayUtil.order(zfPaiPayConfig, 1, userId, orderNumber, amount, body, body);
            }else if(payChannelConfig.getChannelId() == 2){   //微信
                result = ZFPaiPayUtil.order(zfPaiPayConfig, 0, userId, orderNumber, amount, body, body);
            }
            url = result.get("rt10_appPayUrl")+"";

        }else if(payChannelConfig.getType() == 2){  //云支付
            YFKPayConfig yfkPayConfig = (YFKPayConfig) getPayConfig(payChannelConfig);

            url = YFKPayUtil.order(yfkPayConfig, orderNumber, amount, body);

        }else if(payChannelConfig.getType() == 3){  //尊付
            ZunPayConfig zunPayConfig = (ZunPayConfig) getPayConfig(payChannelConfig);

            url = ZunPayUtil.order(zunPayConfig, orderNumber, amount, body);

        }else if(payChannelConfig.getType() == 4){  //96支付
            Pay96Config pay96Config = (Pay96Config) getPayConfig(payChannelConfig);

            String json = Pay96Util.order(pay96Config, orderNumber, amount, body);
            Map<String, Object> jsonMap = JSONObject.parseObject(json, Map.class);
            url = jsonMap.get("payurl")+"";

        }else if(payChannelConfig.getType() == 5){  //付易来
            FYLPayConfig fylPayConfig = (FYLPayConfig) getPayConfig(payChannelConfig);

            String json = null;
            if(payChannelConfig.getChannelId() == 1) {  //支付宝
                json = FYLPayUtil.order(fylPayConfig, 1, orderNumber, amount, body);
            }else if(payChannelConfig.getChannelId() == 2){     //微信
                json = FYLPayUtil.order(fylPayConfig, 0, orderNumber, amount, body);
            }
            Map<String, Object> map = JSONObject.parseObject(json, Map.class);
            url = map.get("payurl")+"";
        }

        if(!StrUtil.isEmptyIfStr(url)){
            order.put("url", url);
        }
        Map<String, Object> data = new HashMap();
        data.put("order", order);
        return data;
    }

    /**
     * 支付回调处理
     * @param request
     * @param payWay 支付类型，1-支付宝  2-微信  3-苹果内购  100-支付派  101-云支付  102-尊付
     * @param receipt   苹果支付凭证
     * @param notify
     * @throws Exception
     */
    public String notifyHandle(HttpServletRequest request, Integer payWay, String receipt, Notify notify) throws Exception{
        String result = null;
        Map<String, String> params = null;

        if(payWay != 3){
            String orderNumber = null;
            if(payWay == 1){    //支付宝
                params = AliPayUtil.getNotifyRequestParams(request);
                orderNumber = (String)params.get("out_trade_no");

            }else if(payWay == 2){  //微信
                params = WxPayUtil.getNotifyRequestParams(request);
                orderNumber = (String)((Map)params).get("out_trade_no");

            }else if(payWay == 100){    //支付派
                params = ZFPaiPayUtil.getNotifyRequestParams(request);
                orderNumber = (String)params.get("rt2_orderId");

            }else if(payWay == 101){   //云支付
                params = YFKPayUtil.getNotifyRequestParams(request);
                orderNumber = (String)params.get("orderNo");

            }else if(payWay == 102){   //尊付
                params = ZunPayUtil.getNotifyRequestParams(request);
                orderNumber = (String)params.get("mch_order_id");

            }else if(payWay == 103){   //96支付
                params = Pay96Util.getNotifyRequestParams(request);
                orderNumber = (String)params.get("orderid");

            }else if(payWay == 104){   //付易来
                params = FYLPayUtil.getNotifyRequestParams(request);
                orderNumber = (String)params.get("out_trade_no");
            }

            SysOrder sysOrder = sysOrderMapper.selectByOrderNumber(orderNumber);
            PayChannelConfig payChannelConfig = payChannelConfigMapper.selectById(sysOrder.getPayChannelConfigId());

            if(payWay == 1){
                AliPayConfig aliPayConfig = (AliPayConfig) getPayConfig(payChannelConfig);
                result = AliPayUtil.verifyNotifyRequest(params, aliPayConfig, payChannelConfig.getAlipayPublicKeyType(), notify);

            }else if(payWay == 2){
                WxPayConfig wxPayConfig = (WxPayConfig) getPayConfig(payChannelConfig);
                result = WxPayUtil.verifyNotifyRequest(params, wxPayConfig, notify);

            }else if(payWay == 100){
                ZFPaiPayConfig zfPaiPayConfig = (ZFPaiPayConfig) getPayConfig(payChannelConfig);
                result = ZFPaiPayUtil.verifyNotifyRequest(params, zfPaiPayConfig, notify);

            }else if(payWay == 101){
                YFKPayConfig yfkPayConfig = (YFKPayConfig) getPayConfig(payChannelConfig);
                return YFKPayUtil.verifyNotifyRequest(params, yfkPayConfig, notify);

            }else if(payWay == 102){
                ZunPayConfig zunPayConfig = (ZunPayConfig) getPayConfig(payChannelConfig);
                return ZunPayUtil.verifyNotifyRequest(params, zunPayConfig, notify);

            }else if(payWay == 103){
                Pay96Config pay96Config = (Pay96Config) getPayConfig(payChannelConfig);
                return Pay96Util.verifyNotifyRequest(params, pay96Config, notify);

            }else if(payWay == 104){
                FYLPayConfig fylPayConfig = (FYLPayConfig) getPayConfig(payChannelConfig);
                return FYLPayUtil.verifyNotifyRequest(params, fylPayConfig, notify);
            }

        }else{
            ApplePayConfig applePayConfig = new ApplePayConfig() {
                @Override
                public String getPayVerifyUrl() {
                    return applePayVerifyUrl;
                }
            };

            boolean flag = ApplePayUtil.notify(applePayConfig, receipt, notify);
            if(flag){
                result = JSONObject.toJSONString(Result.returnSuccess(null));
            }else{
                result = JSONObject.toJSONString(Result.returnFailure());
            }
        }

        return result;
    }

    /**
     * 获取支付渠道状态
     * @param userId    用户id
     * @return
     *  alipay 0-不显示  1-显示
     *  wxpay  0-不显示  1-显示
     * @throws Exception
     */
    public Map<String, Object> getPayStatus(Integer userId) throws Exception{
        Map<String, Object> pay = new HashMap<>();
        pay.put("alipay", 0);
        pay.put("wxpay", 0);
        User user = userMapper.selectById(userId);

        /************************判断微信支付状态*********************************/
        UserSet userSet = userSetMapper.selectByUserId(userId);
        Integer wxPay = userSet == null ? 0 : userSet.getWxPay();
        PayChannel wxPayChannel = payChannelMapper.getAppWxPayStatus();
        if(wxPay == -1){    //禁用该用户微信支付
            pay.put("wxpay", 0);    //不显示
        }else if(wxPay == 0){   //首次支付
            if(wxPayChannel.getStatus() == 0){
                pay.put("wxpay", 0);    //不显示
            }else{
                String ids = sysConfigMapper.getValue("default_pay_config_ids");
                String[] idArr = ids.split(",");
                for(String  id : idArr){
                    PayChannelConfig payChannelConfig = payChannelConfigMapper.selectById(Integer.parseInt(id));
                    if(payChannelConfig.getChannelId() == 2 && payChannelConfig.getStatus() == 0){
                        pay.put("wxpay", 1);
                        break;
                    }
                }
            }
        }else{  //已绑定支付
            if(wxPayChannel.getStatus() == 0){
                pay.put("wxpay", 0);    //不显示
            }else{
                PayChannelConfig payChannelConfig = payChannelConfigMapper.selectById(wxPay);
                if(payChannelConfig.getStatus() == 0){
                    pay.put("wxpay", 1);
                }else{
                    int wxPayCount = payChannelMapper.getAppWxPayConfigCount();
                    if(wxPayCount > 0){
                        pay.put("wxpay", 1);
                    }else{
                        pay.put("wxpay", 0);    //禁用
                    }
                }
            }
        }

        /************************判断支付宝支付状态*********************************/
        Integer aliPay = userSet == null ? 0 : userSet.getAliPay();
        PayChannel aliPayChannel = payChannelMapper.getAppAliPayStatus();
        if(aliPay == -1){    //禁用该用户支付宝支付
            pay.put("alipay", 0);    //不显示
        }else if(aliPay == 0){   //首次支付
            if(aliPayChannel.getStatus() == 0){
                pay.put("alipay", 0);    //不显示
            }else{
                String ids = sysConfigMapper.getValue("default_pay_config_ids");
                String[] idArr = ids.split(",");
                for(String  id : idArr){
                    PayChannelConfig payChannelConfig = payChannelConfigMapper.selectById(Integer.parseInt(id));
                    if(payChannelConfig.getChannelId() == 1 && payChannelConfig.getStatus() == 0){
                        pay.put("alipay", 1);
                        break;
                    }
                }
            }
        }else{  //已绑定支付
            if(aliPayChannel.getStatus() == 0){
                pay.put("alipay", 0);    //不显示
            }else{
                PayChannelConfig payChannelConfig = payChannelConfigMapper.selectById(aliPay);
                if(payChannelConfig.getStatus() == 0){
                    pay.put("alipay", 1);
                }else{
                    int aliPayCount = payChannelMapper.getAppAliPayConfigCount();
                    if(aliPayCount > 0){
                        pay.put("alipay", 1);
                    }else{
                        pay.put("alipay", 0);    //禁用
                    }
                }
            }
        }

        return pay;
    }

    /**
     * 获取支付方式的特定渠道
     * @param userId    用户id
     * @param payWay    充值类型，0-微信  1-支付宝  2-苹果内购
     * @return
     * @throws Exception
     */
    public PayChannelConfig getPayConfig(Integer userId, Integer payWay) throws Exception{
        PayChannelConfig payChannelConfig = null;
        User user = userMapper.selectById(userId);

        //获取默认支付渠道集合
        List<Integer> configIds = new ArrayList<>();
        String ids = sysConfigMapper.getValue("default_pay_config_ids");
        String[] idArr = ids.split(",");
        for(String id : idArr){
            configIds.add(Integer.parseInt(id));
        }

        //优先使用第三方支付
        Integer priorUseThirdPay = Integer.parseInt(sysConfigMapper.getValue("prior_use_third_pay"));
        UserSet userSet = userSetMapper.selectByUserId(userId);

        if(payWay == 0){    //微信支付
            Integer wxPay = userSet == null ? 0 : userSet.getWxPay();
            if(wxPay == 0){ //首次支付
                if(priorUseThirdPay == 1){
                    payChannelConfig = payChannelConfigMapper.selectChannelOne(configIds, 2, -1);
                }else{
                    payChannelConfig = payChannelConfigMapper.selectChannelOne(configIds, 2, null);
                }
            }else if(wxPay > 0){    //已绑定渠道
                payChannelConfig = payChannelConfigMapper.selectById(wxPay);
            }

        }else if(payWay == 1){  //支付宝支付
            Integer aliPay = userSet == null ? 0 : userSet.getAliPay();
            if(aliPay == 0){    //首次支付
                if(priorUseThirdPay == 1){
                    payChannelConfig = payChannelConfigMapper.selectChannelOne(configIds, 1, -1);
                }else {
                    payChannelConfig = payChannelConfigMapper.selectChannelOne(configIds, 1, null);
                }
            }else if(aliPay > 0){   //已绑定渠道
                payChannelConfig = payChannelConfigMapper.selectById(aliPay);
            }

        }else if(payWay == 2){  //苹果内购
            payChannelConfig = payChannelConfigMapper.selectChannelOne(configIds, 3, null);
        }

        //渠道限额
        if(payChannelConfig.getStatus() == 0){
            if(payChannelConfig.getTotalAmount() != 0){
                //渠道累计今日充值
                int totalConfigAmount = sysOrderMapper.getTotalAmount(payChannelConfig.getId(), DateUtil.getCurrBeginTime(), DateUtil.getCurrEndTime());
                if(totalConfigAmount >= payChannelConfig.getTotalAmount()){   //已达到该渠道今日
                    //更新渠道状态
                    payChannelConfig.setStatus(2);    //临时关闭渠道
                    payChannelConfigMapper.updateById(payChannelConfig);

                    if(priorUseThirdPay == 1){
                        payChannelConfig = payChannelConfigMapper.selectChannelOne(configIds, payChannelConfig.getChannelId(), -1);
                    }else{
                        payChannelConfig = payChannelConfigMapper.selectChannelOne(configIds, payChannelConfig.getChannelId(), null);
                    }
                }
            }
        }else{
            if(priorUseThirdPay == 1){
                payChannelConfig = payChannelConfigMapper.selectChannelOne(configIds, payChannelConfig.getChannelId(), -1);
            }else{
                payChannelConfig = payChannelConfigMapper.selectChannelOne(configIds, payChannelConfig.getChannelId(), null);
            }
        }

        //充值金额大于88元的行为超过5次即为优质用户，优质用户可使用对公打款
        int high = userRechargeLogMapper.getUserHighRechargeLogCount(userId);
        if(high >= 5){
            //原生支付
            if(payChannelConfig.getType() != 0){
                payChannelConfig = payChannelConfigMapper.selectChannelOne(configIds, payChannelConfig.getChannelId(), 0);
            }
        }

        return payChannelConfig;
    }
}
